<template>
	<div>
		<div class="padding">
			<el-descriptions :title="nowRowData.organizationName">
				<el-descriptions-item>
					<span>本月第 {{formModel.checkTotal}} 次抽查</span>
				</el-descriptions-item>
				<el-descriptions-item label="地址" :span="2">
					{{ nowRowData.names }}
				</el-descriptions-item>
				<el-descriptions-item label="摄像头编号">
					{{ nowRowData.videoNum }}
				</el-descriptions-item>
				<el-descriptions-item label="位置">
					{{ nowRowData.videoLocationLabel }}
				</el-descriptions-item>
			</el-descriptions>
		</div>

		<AplidForm ref="formRef" v-bind="spotCheckForm" v-model="formModel" @submit="onSubmit" @reset="onReset"> </AplidForm>
	</div>
</template>

<script setup lang="ts">
import { spotCheckForm } from '../config'
import apiMod from '@/apiMod/index'
import { getStore } from '@/utils/store'
import { ElMessage } from 'element-plus'
import { useNow, useDateFormat } from '@vueuse/core'

const props = withDefaults(defineProps<{ nowRowData: any }>(), {})

const emit = defineEmits(['refresh'])

const { now } = useNow({ controls: true })

const formatted = useDateFormat(now, 'YYYY-MM-DD HH:mm:ss')

const formModel = ref(props.nowRowData)

const onReset = () => {
	emit('refresh')
}

const onSubmit = (form, done) => {
	const userInfo = getStore('userInfo') || {}
	const query = {
		...props.nowRowData,
		id: undefined,
		rectificationStatus: 2,
		scTime: formatted.value,
		scUserId: userInfo.id,
		scUserName: userInfo.realName,
		videoContent: form.videoContent,
		videoPic: form.videoPic,
		videoNum: form.videoNum,
	}
	apiMod.view_scSpotCheckMonitor_add(query).then(() => {
		ElMessage.success('添加成功')
		onReset()
		done()
	})
}
const getMonthLastDateFn = (year,month)=>{
	
    // let dateStr = '2020-02-06'; //需要获取此月最后一天的日期
    let dateObj = new Date(year+'-'+month+'-01');
    let nextMonth = dateObj.getMonth()+1; //0-11，下一个月
    //设置当前日期为下个月的1号
    dateObj.setMonth(nextMonth);
    dateObj.setDate(1);  //1-31

    let nextMonthFirstDayTime = dateObj.getTime(); //下个月一号对应毫秒

    let theMonthLastDayTime = nextMonthFirstDayTime-24*60*60*1000;  //下个月一号减去一天，正好是这个月最后一天

    let theMonthDay = (new Date(theMonthLastDayTime)).getDate();
    return year+'-'+month+'-'+theMonthDay;
  }

onMounted(()=> {
  const d = new Date();
  const year = d.getFullYear()
  const m =d.getMonth()+1
  const mm = m<10 ? '0'+m : m
  const scTimeBetween = `${year}-${mm}-01 00:00:00,${getMonthLastDateFn(year,mm)} 23:59:59`
  apiMod.view_scSpotCheckMonitor_page({
    creditCode:props.nowRowData.creditCode,
    scTimeBetween
  }).then(res=> {
    const data = res.data
    formModel.value.checkTotal = data.total 
  })
})
</script>

<style scoped></style>
